﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hbse.Entities;
using NHibernate;
using hbse.DTO;

namespace hbse.Service
{
    public class SampleFolderMService
    {
        public List<hbse.DTO.ProductDTOSampleF> searchByCode(string _productCode, ISession session)
        {

            List<ProductDTOSampleF> productDTOSampleFs = new List<ProductDTOSampleF>();
            ProductDTOSampleF productDTOSampleF = null;

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode='" + _productCode + "'").Future<Product>();
                foreach (Product p in products)
                {
                    productDTOSampleF = new ProductDTOSampleF();
                    productDTOSampleF.Id = p.Id;
                    productDTOSampleF.ProductCode = p.ProductCode;
                    productDTOSampleF.CustomerCode = p.CustomerProductCode;
                    productDTOSampleFs.Add(productDTOSampleF);
                }

            }

            return productDTOSampleFs;
        }

        public Product getByCode(string _productCode, ISession session)
        {
            Product product = new Product();

            using (session.BeginTransaction())
            {

                var products = session.CreateQuery("from Product p where p.ProductCode='" + _productCode + "'").Future<Product>();
                if (products.Count()>0)
                {
                    product = products.First();
                }
               

            }

            return product;
        }

        public List<hbse.DTO.CustomerDtoSampleF> getCustomerAll(ISession session)
        {

            List<CustomerDtoSampleF> customerDtoSampleFs = new List<CustomerDtoSampleF>();
            CustomerDtoSampleF customerDtoSampleF = null;

            using (session.BeginTransaction())
            {

                var customers = session.CreateQuery("from Customer ").Future<Customer>();
                foreach (Customer c in customers)
                {
                    customerDtoSampleF = new CustomerDtoSampleF();
                    customerDtoSampleF.Id = c.Id;
                    customerDtoSampleF.Code = c.CustomerCode;
                    customerDtoSampleF.Name = c.CustomerName;
                    customerDtoSampleFs.Add(customerDtoSampleF);
                }

            }

            return customerDtoSampleFs;
        }
        public int AddProduct(Product newproduct,ISession session) {
            int result = -1;

            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(newproduct);
                session.Flush();
                tx.Commit();
                result = 1;
            }

            return result;
        
        }
        public int editProduct(Product editproduct, ISession session)
        {
            int result = -1;

            using (ITransaction tx = session.BeginTransaction())
            {
                session.SaveOrUpdate(editproduct);
                session.Flush();
                tx.Commit();
                result = 1;
            }

            return result;

        }
        public int delProduct(Product delproduct, ISession session)
        {
            int result = -1;

            using (ITransaction tx = session.BeginTransaction())
            {
                session.SaveOrUpdate(delproduct);
                session.Flush();
                tx.Commit();
                result = 1;
            }

            return result;

        }

        public int AddProductPrint(ProductPrint newproductPrint, ISession session)
        {
            int result = -1;

            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(newproductPrint);
                session.Flush();
                tx.Commit();
                result = 1;
            }

            return result;

        }
    }
}
